package com.hmy.equipment.controller;


import com.hmy.equipment.domain.HmyGas;
import com.hmy.equipment.service.AeviceState;
import com.hmy.equipment.service.HmyGasService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.constant.TableName;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 智能气体检测仪信息
 *
 * @author kdq
 */
@RestController
@RequestMapping("/gas")
public class HmyGasController extends BaseController {

    @Autowired
    private HmyGasService gasService;
    @Resource
    private AeviceState aeviceState;

    /**
     * 添加气体检测仪
     *
     * @param gas 气体检测仪实体类
     * @return
     */
    @Log(title = "气体检测仪设备添加", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Boolean> addGas(HmyGas gas) {
        gas.setCreatedBy(SecurityUtils.getUsername());
        gas.setUpdatedBy(SecurityUtils.getUsername());
        Boolean b = gasService.addGas(gas);
        aeviceState.getPhouseCoudInTable(TableName.GAS, gas.getPhouseCode());
        return R.ok(b);
    }

    /**
     * 根据检测仪列表条件查询
     *
     * @param gas 气体检测仪实体类
     * @return 分页列表
     */
    @PostMapping("/list")
    public TableDataInfo selectGasList(HmyGas gas) {
        System.out.println(gas);
        List<HmyGas> list = gasService.selectGasList(gas);
        return getDataTable(list);
    }

    /**
     * 根据id单体查询
     *
     * @param id id
     * @return 实体类数据
     */
    @PostMapping("/listById/{id}")
    public AjaxResult selectGasListAll(@PathVariable("id") Long id) {
        return AjaxResult.success(gasService.selectGasListById(id));
    }

    /**
     * 气体检测仪设备修改
     *
     * @param gas 检测仪实体类
     * @return
     */
    @Log(title = "气体检测仪设备修改", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public R<Boolean> editGas(HmyGas gas) {
        gas.setUpdatedBy(SecurityUtils.getUsername());
        Boolean b = gasService.editGas(gas);
        aeviceState.getPhouseCoudInTable(TableName.GAS, gas.getPhouseCode());
        return R.ok(b, "修改成功");
    }

    /**
     * 气体检测仪设备删除
     *
     * @param ids id主键
     * @return
     */
    @Log(title = "气体检测仪设备删除", businessType = BusinessType.DELETE)
    @PostMapping("/del/{ids}")
    public R<Boolean> delGas(@PathVariable("ids") long[] ids) {
        String phouseCode = gasService.selectGasListById(ids[0]).getPhouseCode();
        Boolean b = gasService.delGas(ids);
        aeviceState.getPhouseCoudInTable(TableName.GAS, phouseCode);
        return R.ok(b, "已删除");
    }
}
